package com.rt.tmpt.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.*;


public class DateTool {
    private static long MSEC_EVERYDAY = 86400000L; // 一天的微秒数


    public static Calendar getCalendarInstance() {
        //TimeZone timeZone = TimeZone.getTimeZone("GMT+08:00");
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        return Calendar.getInstance(timeZone);
    }


    public static long getDaysBetween2Date(Date d1, Date d2) throws ParseException {
        //convert d1 to calendar1
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(d1);
        //convert d2 to calendar2
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(d2);
        //compare two calendar(calendar1,calendar)
        long count = (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / MSEC_EVERYDAY;

        return count;
    }

    public static long getDaysBetween2Date(String s1, String s2) throws ParseException {
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        //convert s1 to d1
        Date d1 = sf.parse(s1);
        //convert s2 to d2
        Date d2 = sf.parse(s2);

        return DateTool.getDaysBetween2Date(d1, d2);
    }

    public static long getDaysBetween2Date(Date d, int i) throws ParseException {
        //取当前日期
        Date currentDate = DateTool.getCurrentDate();
        long count = 0;
        if (i == 1) {
            count = DateTool.getDaysBetween2Date(d, currentDate);
        } else if (i == 2) {
            count = DateTool.getDaysBetween2Date(currentDate, d);
        }
        return count;
    }

    //convert date to string
    public static String date2String(Date d) {
        if (d == null || d.equals("")) {
            return "";
        }
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        String s = sf.format(d);
        return s;
    }

    //convert date to string
    public static String date2String(Date d, String format) {
        if (d == null || d.equals("")) {
            return "";
        }
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(format);
        String s = sf.format(d);
        return s;
    }

    //convert String to date
    public static Date string2Date(String s) {
        if (s == null) return null;
        if (s.length() != 10) s = s.substring(0, 10);
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date = sf.parse(s);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //convert String to date
    public static Date string2Date(String s, String format) {
        if (s == null || format == null) return null;
        if (format.length() == 10) {
            if (s.length() < 10) {
                return null;
            } else {
                s = s.substring(0, 10);
            }
        } else if (format.length() == 19) {
            if (s.length() < 10) {
                return null;
            } else if (s.length() == 10) {
                s += " 00:00:00";
            }
        }

        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(format);
        Date date;
        try {
            date = sf.parse(s);
            return date;
        } catch (ParseException e) {
            System.out.println(e.toString());
        }
        return null;
    }

    public static Date string2DateEnd(String s, String format) {
        if (s == null) return null;
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(format + " HH:mm:ss.SSS");
        Date date;
        try {
            if (s.contains(":")) {
                date = sf.parse(s);
            } else {
                date = sf.parse(s + " 23:59:59.999");
            }
            return date;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }


    //convert String to date
    public static Date string2DateForAnyFormat(String dateStr) {
        String formatStr = null;
        int dateStrLen = dateStr.length();
        switch (dateStrLen) {
            case 8: {
                formatStr = "yyyyMMdd";
                break;
            }
            case 10: {
                if (dateStr.charAt(4) == '-') {
                    formatStr = "yyyy-MM-dd";
                } else if (dateStr.charAt(2) == '/') {
                    formatStr = "MM/dd/yyyy";
                }
                break;
            }
        }
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(formatStr);
        Date date;
        try {
            date = sf.parse(dateStr);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //convert String to date
    public static Date string2DateForAnyFormat(String dateStr, String formatStr) {
        if (dateStr == null) return null;

        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(formatStr);
            Date date;
            date = sf.parse(dateStr);
            return date;
        } catch (Exception e) {
            //e.printStackTrace();
        }

        return null;
    }


    //get current date
    public static Date getCurrentDate() {
        Date currentDate = null;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date currentDate1 = getCalendarInstance().getTime();
            String strDate = sf.format(currentDate1);
            currentDate = sf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return currentDate;
    }

    //get before date
    public static Date getBeforeDate() {
        Date date = null;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Calendar c1 = getCalendarInstance();
            c1.add(Calendar.DATE, -1);
            Date date1 = c1.getTime();
            String strDate = sf.format(date1);
            date = sf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    //get before date
    public static String getBeforeDateStr() {
        String strDate = null;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Calendar c1 = getCalendarInstance();
            strDate = sf.format(getBeforeDate());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }


    //get current datetime
    public static Date getCurrentDatetime() {
        Date currentDate = null;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c1 = getCalendarInstance();
            Date currentDate1 = c1.getTime();
            String strDate = sf.format(currentDate1);
            currentDate = sf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return currentDate;
    }

    //get current datetime
    public static Date getCurrentDatetime(String format) {
        Date currentDate = null;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(format);
            Calendar c1 = getCalendarInstance();
            Date currentDate1 = c1.getTime();
            String strDate = sf.format(currentDate1);
            currentDate = sf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return currentDate;
    }

    //get current date
    public static String getCurrentDateStr() {
        String strDate = null;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Calendar c1 = getCalendarInstance();
            Date currentDate1 = c1.getTime();
            strDate = sf.format(currentDate1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }

    //get current date
    public static String getSpecDateStr(Date date, String format) {
        String strDate = null;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(format);
            strDate = sf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }

    //get current date
    public static String getCurrentDateStr1(String formatStr) {
        String strDate = null;
        try {
            java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(formatStr);
            Calendar c1 = getCalendarInstance();
            Date currentDate1 = c1.getTime();
            strDate = sf.format(currentDate1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }

    /*//取指定日期中月份有多少天
    public static int getDaysFromMonth(String strDate) throws ParseException{
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = getCalendarInstance();
        Date date = sf.parse(strDate);
        calendar.setTime(date);
        calendar.add(calendar.MONTH, 1);
        calendar.set(calendar.DATE, 1);
        calendar.add(calendar.DAY_OF_MONTH, -1);
        Date date1 = calendar.getTime();

        return date1.getDate();
    }*/
	/*//取指定日期到月末有多少天（包括指定日期）
	public static int getDaysBetweenDateToMonthEnd(String strDate) throws ParseException{
		java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = getCalendarInstance();
		Date date = sf.parse(strDate);

		int date1Int = DateTool.getDaysFromMonth(strDate);
		return date1Int-date.getDate()+1;
	}*/
    //取上个月
    public static String getLastMonth() {
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-01");
        Calendar calendar = getCalendarInstance();
        Date date = calendar.getTime();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, -1);
        Date date1 = calendar.getTime();
        String date1Str = sf.format(date1);
        return date1Str;
    }

    //取上个月最后一天
    public static String getLastMonthDate() throws ParseException {
        //取本月第一天
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-01");
        Calendar calendar = getCalendarInstance();
        Date date = calendar.getTime();
        String date1Str = sf.format(date);
        //取上个月最后一天
        Calendar c1 = getCalendarInstance();
        java.text.SimpleDateFormat sf1 = new java.text.SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sf1.parse(date1Str);
        c1.setTime(date1);
        c1.add(c1.DATE, -1);
        Date date2 = c1.getTime();
        date1Str = sf1.format(date2);

        return date1Str;
    }

    public static String getMonthFirstDate() throws ParseException {
        //取本月第一天
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-01");
        Calendar calendar = getCalendarInstance();
        Date date = calendar.getTime();
        String date1Str = sf.format(date);
        return date1Str;
    }

    public static Timestamp getCurTimestamp() {
        return new Timestamp(DateTool.getCurrentDatetime("yyyy-MM-dd HH:mm:ss.SSS").getTime());
    }

    public static Timestamp str2Timestamp(String dateStr) {
        if (dateStr == null) return null;
        if (dateStr.length() == 10) {
            dateStr += " 00:00:00";
        }
        Date date = DateTool.string2Date(dateStr, "yyyy-MM-dd HH:mm:ss");
        return new Timestamp(date.getTime());
    }

    public static void main1(String[] args) {
        //System.out.println(str2Timestamp("2019-11-11","yyyy-MM-dd"));
        //System.out.println(timestampAppend235959999(str2Timestamp("2019-11-11", "yyyy-MM-dd")));
        System.out.println(str2Timestamp(getCurrentDateStr(), "yyyy-MM-dd"));
    }

    public static Timestamp str2Timestamp(String dateStr, String format) {
        Date date = DateTool.string2Date(dateStr, format);
        return new Timestamp(date.getTime());
    }

    public static Timestamp timestampAppend235959999(Timestamp timestamp) {
        if (timestamp == null) return null;
        String dateStr = DateTool.date2String(timestamp, "yyyy-MM-dd 23:59:59.999");
        Timestamp timestamp1 = DateTool.str2Timestamp(dateStr, "yyyy-MM-dd HH:mm:ss.SSS");
        return timestamp1;
    }

    //取当前时间与指定日期有多少天
    public static boolean getDaysBetweenDateToCurrentDate(String strDate2, int date) throws ParseException {
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = getCalendarInstance();
        calendar.add(Calendar.DATE, date);
        Date date1 = calendar.getTime();
        Date date2 = sf.parse(strDate2);
        if (date1.compareTo(date2) > 0) return true;
        else return false;
    }

    //
    public static String Str2Str(Object dateStr, String format) throws ParseException {
        if (dateStr == null || dateStr.toString().trim().equals("")) {
            return "";
        }
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(format);
        Date date = sf.parse(dateStr.toString());
        return sf.format(date);
    }

    public static int compare2CurDate(String dateStr, int hourInt) throws ParseException {
        //时间加上小时
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sf.parse(dateStr);
        Calendar calendar = getCalendarInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hourInt);
        Date dateNew = calendar.getTime();
        //当前时间
        Date dateCur = DateTool.getCurrentDatetime();

        return dateNew.compareTo(dateCur);
    }

    public static String dateAddHour(String dateStr, int hourInt) throws ParseException {
        //时间加上小时
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sf.parse(dateStr);
        Calendar calendar = getCalendarInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hourInt);
        Date dateNew = calendar.getTime();
        String dateNewStr = DateTool.date2String(dateNew, "yyyy-MM-dd HH:mm:ss");
        return dateNewStr;
    }


    public static String dateAddDay(String dateStr, int dayInt) throws ParseException {
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        Date date = sf.parse(dateStr);
        Calendar calendar = getCalendarInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dayInt);
        Date dateNew = calendar.getTime();
        String dateNewStr = sf.format(dateNew);
        return dateNewStr;
    }

    public static Date dateAddDay(Date date, int dayInt) throws ParseException {
        Calendar calendar = getCalendarInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dayInt);
        Date dateNew = calendar.getTime();
        return dateNew;
    }

    public static int compare2CurDateMinuter(Date date, int minuterInt) throws ParseException {
        Calendar calendar = getCalendarInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minuterInt);
        Date dateNew = calendar.getTime();
        System.out.println(dateNew);
        //当前时间
        Date dateCur = DateTool.getCurrentDatetime();
        System.out.println(dateCur);

        return dateNew.compareTo(dateCur);
    }

    public static String currentDate(String strFormat) {
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
                strFormat);
        Calendar cal = getCalendarInstance();

        return format.format(cal.getTime());
    }

    /**
     * 传入年，获取一年时间
     */
    public static List<String> getDaysByYear(int year) {
        System.out.println(year);
        Calendar c = Calendar.getInstance();
        List<String> dates = new ArrayList<String>();
        for (int i = 0; i < 12; i++) {
            c.set(year, i, 1);
            int lastDay = c.getActualMaximum(Calendar.DATE);
            for (int j = 1; j <= lastDay; j++) {
                String month = "";
                String day = "";
                if (i < 9) {
                    month = "-0" + (i + 1);
                } else {
                    month = "-" + (i + 1);
                }
                if (j < 10) {
                    day = "-0" + j;
                } else {
                    day = "-" + j;
                }
                String date = year + month + day;
                System.out.println(date);
                dates.add(date);
            }
        }
        return dates;
    }


    //test
    public static void main(String args[]) throws ParseException {

        List<String> Yeardates = new ArrayList();
        //循环输出2014到2020年每一天
//		for (int year = 2020; year < 2021; year++) {
//			Yeardates.addAll(DateTool.getDaysByYear(year));
//		}
//		for (int i = 0; i < Yeardates.size(); i++) {
//			System.out.println(Yeardates.get(i));
//		}

        List list = null;
        System.out.println(list.size());

        //System.out.println(DateTool.getCurTimestamp());
//        System.out.println(DateTool.timestampAppend235959999(DateTool.getCurTimestamp()));
        //System.out.println(DateTool.getCurrentDatetime("yyyy-MM-dd 00:00:00.000"));
        //System.out.println(DateTool.getCurrentDatetime("yyyy-MM-dd 23:59:59.999"));
        //java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //java.util.Date date = sf.parse("2013-05-12 11:01:00");

        //System.out.println(DateTool.compare2CurDateMinuter(date, 2));
        //System.out.println(DateTool.dateAddDay("2012-11-11", 2));
        //System.out.println(DateTool.compare2CurDate("2012-12-12 10:00:00", 1));;

        //System.out.println(DateTool.string2DateForAnyFormat("03/01/2012"));
		/*
		Object ss = "2010-03-31 17:23:03.29";
		String datestr = DateTool.Str2Str(ss, "yyyy-MM-dd");
		System.out.println(datestr);
		*/
        //		System.out.println(DateTool.getDaysBetweenDateToCurrentDate("2010-01-30",-2));
//		long i = DateTool.getDaysBetween2Date("2009-08-25", "2009-08-30");
//		System.out.println(i);
        //System.out.println(DateTool.getLastMonthDate());
//		System.out.println(DateTool.getDaysBetweenDateToMonthEnd("2009-08-22"));
//		long count = DateTool.getDaysBetween2Date("2009-02-07", "2009-02-08");
//		System.out.println(count);
        //System.out.println(DateTool.string2Date("2009-12-02"));

//		java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy-MM-dd");
//		Calendar c1 = java.util.getCalendarInstance();
//		Date currentDate1 = c1.getTime();
//		String strDate = sf.format(currentDate1);
//		Date currentDate = sf.parse(strDate);
//		System.out.println(currentDate);
    }
}
